﻿package com.kaede.popcake;

import java.io.IOException;

import net.youmi.android.AdManager;
import net.youmi.android.banner.AdSize;
import net.youmi.android.banner.AdView;
import net.youmi.android.spot.SpotManager;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;

import com.actionbarsherlock.app.SherlockActivity;
import com.kaede.util.AppUitil;

public class gifActivity extends SherlockActivity implements OnClickListener {
	/** Called when the activity is first created. */
	com.actionbarsherlock.app.ActionBar action;
	TypegifView gifview;
	Boolean isOver = true;
	Boolean isOk = true;
	private MediaPlayer mMediaPlayer;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		action = getSupportActionBar();
		action.setDisplayHomeAsUpEnabled(true);
		action.setDisplayShowTitleEnabled(false);
		if (AppUitil.changeScreen(this, action.isShowing()))
			action.hide();
		else
			action.show();
		gifview = (TypegifView) findViewById(R.id.gifView1);
		gifview.setZoom(true, AppUitil.getDeviveWidth(this),
				AppUitil.getDeviveHeight(this));
		gifview.setRepeat(true);
		switch (DataHolder.index) {
		case 0:
			gifview.setSrc(R.raw.cake1);
			break;
		case 1:
			gifview.setSrc(R.raw.cake2);
			break;
		case 2:
			gifview.setSrc(R.raw.cake3);
			break;
		case 3:
			gifview.setSrc(R.raw.cake4);  
			break;
		case 4:
			gifview.setSrc(R.raw.cake5);
			break;
		case 5:
			gifview.setSrc(R.raw.cake6);
			break;
		case 6:
			gifview.setSrc(R.raw.cake7);
			break;
		default:
			gifview.setSrc(R.raw.cake1);
			break;
		}
		gifview.setOnClickListener(this);
		new KaedeShake(this, new KaedeShake.IShakeCallback() {
			
			@Override
			public void onShakeIgnore() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onShake() {
				// TODO Auto-generated method stub
				play();
			}
		}).setLevel(3);;
		
		AppUitil.MakeToastShort(this, "试试摇动或触摸");
		
		AdManager.getInstance(this).init("7291f51ce48e4b4c","c2c864aeb51e55cc", false); 

	}

	/** 播放声音 */
	public void startVoice() {
		mMediaPlayer = MediaPlayer.create(this, R.raw.shake);
		mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub

			}
		});
		if (mMediaPlayer.isPlaying()) {
			return;
		} else {
			mMediaPlayer.start();
		}
	}

	public void play() {
		if (isOk) {
			startVoice();
			if (isOver) {
				isOver = false;
				gifview.setStart();
				new Thread() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						super.run();
						try {
							sleep(2000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						gifview.setStop();
						isOver = true;
					}
				}.start();
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		// TODO Auto-generated method stub
		getSupportMenuInflater().inflate(R.menu.gif, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_full:
			if (AppUitil.changeScreen(this, action.isShowing()))
				action.hide();
			else
				action.show();
			break;
		case R.id.menu_nfull:
			if (AppUitil.changeScreen(this, action.isShowing()))
				action.hide();
			else
				action.show();
			break;
		case R.id.menu_play:
			gifview.setStart();
			break;
		case R.id.menu_stop:
			gifview.setStop();
			break;
		case R.id.menu_wall:
			try {
				setWallpaper(gifview.gHelper.getImage());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			AppUitil.MakeToastShort(this, "一般壁纸可是当前的帧数哦");
			break;
		case android.R.id.home:
			//AppUitil.MakeToastShort(this, "在线高清资源请更新最新版…");
			//SpotManager.getInstance(this).showSpotAds(this);
			break;
		case R.id.menu_livewall:
			String gif;
			switch (DataHolder.index) {
			case 0:
				gif = String.valueOf(R.raw.cake1);
				break;
			case 1:
				gif = String.valueOf(R.raw.cake2);
				break;
			case 2:
				gif = String.valueOf(R.raw.cake3);
				break;
			case 3:
				gif = String.valueOf(R.raw.cake4);
				break;
			case 4:
				gif = String.valueOf(R.raw.cake5);
				break;
			case 5:
				gif = String.valueOf(R.raw.cake6);
				break;
			case 6:
				gif = String.valueOf(R.raw.cake7);
				break;
			default:
				gif = String.valueOf(R.raw.cake1);
				break;
			}
			SharedPreferences sp = this.getSharedPreferences("PREF",
					Context.MODE_WORLD_READABLE);
			Editor editor = sp.edit();
			editor.putString("gif",gif);
			// 必须要提交，否则增加不了数据
			editor.commit();
			startActivity(new Intent().setAction(WallpaperManager
					.getInstance(this).ACTION_LIVE_WALLPAPER_CHOOSER));
			break;
		case R.id.menu_setting:
			startActivity(new Intent().setClass(this, Preference.class));
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		this.isOk=false;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		this.isOk=true;
		loadAd();
		super.onResume();
		
	}
	public void loadAd()
	{	
		//实例化LayoutParams(重要)
	    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
	        FrameLayout.LayoutParams.FILL_PARENT,
	        FrameLayout.LayoutParams.WRAP_CONTENT);     
	    //设置广告条的悬浮位置
	    layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; // 这里示例为右下角  
	    //实例化广告条
	    AdView adView = new AdView(this, AdSize.FIT_SCREEN);
	    //调用Activity的addContentView函数
	    this.addContentView(adView, layoutParams);   
	    //SpotManager.getInstance(this).loadSpotAds();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		play();
		
	}
}
